package com.atguigu.order.controller;

import com.atguigu.utils.R;
import com.atguigu.order.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@RestController
@CrossOrigin
@Api(value = "TPayLogController", description = "8007支付接口")
@RequestMapping("/order/tpaylog")
@Slf4j
public class TPayLogController {

    @Autowired
    TPayLogService tPayLogService;

    //    生成订单二维码
    @ApiOperation("根据订单号生成二维码")
    @PostMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {

        HashMap<String, String> map = tPayLogService.createNative(orderNo);

        return R.ok().data("native", map);
    }

    //    查询订单状态
    @PostMapping("/getOrderStatus/{orderNo}")
    public R getOrderStatus(@PathVariable String orderNo) {
//        获取支付结果 返回微信端响应的数据
        Map<String, String> map = tPayLogService.getStatus(orderNo);
//        获取支付结果 返回微信端响应的数据
        log.debug(map.toString());

        if ("SUCCESS".equals(map.get("trade_state"))) {
//            支付成功
            log.info(map.get("trade_state"));
            String s = tPayLogService.payLogUpdate(map);
            log.info(s);
//            记录添加成功!
            if ("OK".equals(s)) {
                return R.ok().code(20000).message("支付成功!");
            }
        }
        if (map == null) {
//            支付失败
            return R.error().message("支付失败").code(20001);
        }
        return R.ok().code(25000).data("status", "支付中");
    }
}

